/**
 * 
 * The MIT License
 * 
 * Copyright (c) 2008 the original author or authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.gchartjava;

import static java.util.Collections.unmodifiableList;

import java.util.LinkedList;
import java.util.List;

/**
 * Common code for line charts exists here.
 * 
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
abstract class AbstractLineChart extends AbstractMarkableChart {

	private static final String LINE_STYLE_PARAM = "chls=";

	private final List<? extends Plot> plots;

	/**
	 * 
	 */
	AbstractLineChart(final List<? extends Plot> lines) {
		super();
		this.plots = lines;
	}

	/**
	 * Internal method for setting the styles of the line.
	 */
	private void setLineStyle(final List<LineStyle> lineStyles) {
		final StringBuilder lineStyleParam = new StringBuilder();
		int count = 0;
		for (LineStyle lineStyle : lineStyles) {
			if (lineStyle == null)
				continue;
			lineStyleParam.append((count++ == 0) ? lineStyle : "|" + lineStyle);
		}
		if (count > 0) {
			addParameter(LINE_STYLE_PARAM, lineStyleParam.toString());
		}
	}
	
	/**
	 * 
	 */
	@Override
	protected void prepareDataInternal() {
		final List<String> legends                           = new LinkedList<String>();
		final List<Color> colors                             = new LinkedList<Color>();
		final List<LineStyle> lStyles                        = new LinkedList<LineStyle>();
		final List<List<? extends ShapeMarker>> shapeMarkers = new LinkedList<List<? extends ShapeMarker>>();
		final List<Color> fillAreaColors                     = new LinkedList<Color>();

		// Logic to make sure things stay in step.
		boolean hasLegend         = false;
		boolean hasColor          = false;
		boolean hasLineStyle      = false;
		boolean hasShapeMarkers   = false;
		boolean hasFillAreaColors = false;

		// Logic to make sure things stay in step continued.
		for (Plot plot : plots) {
			hasLegend         |= (plot.getLegend() != null);
			hasColor          |= (plot.getColor() != null);
			hasLineStyle      |= (plot.getLineStyle() != null);
			hasShapeMarkers   |= (plot.getShapeMarkers().size() > 0);
			hasFillAreaColors |= (plot.getFillAreaColor() != null);
		}

		// Logic to make sure things stay in step continued.
		for (Plot line : plots) {
			if (hasLegend) {
				legends.add(line.getLegend() != null ? line.getLegend() : "+");
			}
			if (hasColor) {
				colors.add(line.getColor() != null ? line.getColor() : Color.BLACK);
			}
			if (hasLineStyle) {
				// TODO: Create some static line styles.
				lStyles.add(line.getLineStyle() != null ? line.getLineStyle() : new LineStyle(1, 1, 0));
			}
			if (hasShapeMarkers) {
				shapeMarkers.add(line.getShapeMarkers().size() > 0 ? line.getShapeMarkers() : null);
			}
			if (hasFillAreaColors) {
				fillAreaColors.add(line.getFillAreaColor());
			}
		}

		setLegends(unmodifiableList(legends));
		setColors(unmodifiableList(colors));
		setLineStyle(unmodifiableList(lStyles));
		prepareLineData();
		setAxisLabels();
		setShapeMarkers(unmodifiableList(shapeMarkers));
		setFillAreaColors(unmodifiableList(fillAreaColors));
	}
	
	/**
	 * Prepare line data individually for subtypes as this process is different
	 * for different types of charts.
	 */
	protected abstract void prepareLineData();

	/**
	 * 
	 */
	protected final List<? extends Plot> getLines() {
		return plots;
	}
}
